package mi.payment.bean.settings;

/**
 * 安全密钥区域分配
 *
 * @author Xiho on 2017/1/19.
 */
public class SecurityKeyAreaDistribution {

    // 3DES偏移量：41
    private static final int _3DES_OFFSET = 41;
    // DES偏移量：1
    private static final int DES_OFFSET = 1;
    // 主密钥偏移量：0
    private static final int TMK_OFFSET = 0;
    // pik偏移量：10
    private static final int PIK_OFFSET = 10;
    // MAK偏移量：20
    private static final int MAK_OFFSET = 20;
    // TDK偏移量：30
    private static final int TDK_OFFSET = 30;
    // KEK偏移量：81
    private static final int KEK_OFFSET = 81;

    private static final String _3DES = "3DES";
    private static final String _DES = "DES";

    /**
     * 得到主密钥的索引
     */
    public static int getTMKIndex(String encryptType, int index) {
        int tmkIndex = 0;
        if (encryptType.equals(_3DES)) {
            tmkIndex = index + _3DES_OFFSET + TMK_OFFSET;
        } else if (encryptType.equals(_DES)) {
            tmkIndex = index + DES_OFFSET + TMK_OFFSET;
        }
        return tmkIndex;
    }


    /**
     * 得到MAK的密钥索引
     */
    public static int getMAKIndex(String encryptType, int index) {
        int makIndex = MAK_OFFSET;
        if (encryptType.equals(_3DES)) {
            makIndex = index + _3DES_OFFSET + MAK_OFFSET;
        } else if (encryptType.equals(_DES)) {
            makIndex = index + DES_OFFSET + MAK_OFFSET;
        }
        return makIndex;
    }

    /**
     * 得到PIK的密钥索引
     */
    public static int getPIKIndex(String encryptType, int index) {
        int pikIndex = PIK_OFFSET;
        if (encryptType.equals(_3DES)) {
            pikIndex = index + _3DES_OFFSET + PIK_OFFSET;
        } else if (encryptType.equals(_DES)) {
            pikIndex = index + DES_OFFSET + PIK_OFFSET;
        }
        return pikIndex;
    }


    /**
     * 得到TDK的密钥索引
     */
    public static int getTDKIndex(String encryptType, int index) {
        int tdkIndex = TDK_OFFSET;
        if (encryptType.equals(_3DES)) {
            tdkIndex = index + _3DES_OFFSET + TDK_OFFSET;
        } else if (encryptType.equals(_DES)) {
            tdkIndex = index + DES_OFFSET + TDK_OFFSET;
        }
        return tdkIndex;
    }

    /**
     * 得到KEK的索引
     */
    public static int getKEKIndex(int index) {
        return index + KEK_OFFSET;
    }


}
